package com.feng.study.demo.datetime;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.Date;

/**
 * java8,新的类LocalDate、LocalDateTime、、、
 *
 * 1.LocalDate获取年月日
 * 2.LocalTime获取时分秒
 * 3.LocalDateTime获取年月日、时分秒。相当于前面两个的集合
 */
@Slf4j
@Service
public class TimeDemo {

    /**
     * 日期格式
     */
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static void main(String[] args) {
        demoLocalDate();
        System.out.println("========================");
        demoLocalTime();
        System.out.println("========================");
        demoLocalDateTime();
    }
    private static void demoLocalDate(){
        //年月日，日期，星期
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        System.out.println(localDate.getYear());
        System.out.println(localDate.getMonth());
        System.out.println(localDate.getDayOfMonth());
        System.out.println(localDate.getDayOfWeek());
        System.out.println(localDate.getDayOfYear());
    }
    private static void demoLocalTime(){
        //时分秒
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);
        System.out.println(localTime.getHour());
        System.out.println(localTime.getMinute());
        System.out.println(localTime.getSecond());
        //纳秒（1毫秒等于1000微秒，等于1000000纳秒）
        System.out.println(localTime.getNano());
        System.out.println(localTime.getLong(ChronoField.HOUR_OF_DAY));
    }
    private static void demoLocalDateTime(){
        //年月日、时分秒
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        //一个月前
        System.out.println(localDateTime.minusMonths(1));
        //自定义日期2021-12-31T12:12:30
        LocalDateTime of = LocalDateTime.of(2021, 12, 31, 12, 12, 30);
        //12-31的前一个月没有31号，所以是11-30
        // 2021-11-30T12:12:30
        LocalDateTime localDateTime1 = of.minusMonths(1);
        System.out.println(localDateTime1);

        //格式化
        String format = localDateTime.format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
        System.out.println(format);
    }
}
